package main;

import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ShootUI extends PhaseUI {
	
	public ShootMouse myMouse;
	public Panel targetStats;
	public Button shootButton;
	
	public void beginPhase() {
		myUI.targetedUnit = null;
		
		myMouse = new ShootMouse();
		myMouse.myUI = myUI;
		myUI.addMouseListener(myMouse);
		myUI.addMouseMotionListener(myMouse);
		
		targetStats = myUI.buildStatsPanel("Target", myUI.dim.width-200, myUI.dim.height-280);
		myUI.add(targetStats);
		
		shootButton = new Button("Shoot");
		shootButton.setName("Shoot");
		shootButton.setBounds(myUI.dim.width-100,50,100,50);
		shootButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(myUI.selectedUnit != null && myUI.targetedUnit != null){
					setShooting(true);
					myUI.selectedUnit.shootAt(myUI.targetedUnit);
				}
			}
		});
		myUI.add(shootButton);
		
		myUI.revalidate();
		myUI.repaint();
	}
	
	public void setShooting(boolean shooting) {
		this.shootButton.setEnabled(!shooting);
		this.myUI.endPhase.setEnabled(!shooting);
	}
	
	public void endPhase() {
		// If we're actually switching phases, do the cleanup and swap out for a new PhaseUI
		if (myUI.game.priority != myUI.game.currArmy){
			// Clean up shoot stuff
			myUI.removeMouseListener(myMouse);
			myUI.removeMouseMotionListener(myMouse);
			
			// Clean up target panel
			myUI.remove(targetStats);
			myUI.remove(shootButton);
			
			// Create a FightUI and set it to be the new phase
			HeroicFightUI fightUI = new HeroicFightUI();
			fightUI.myUI = myUI;
			myUI.phaseUI = fightUI;
			myUI.phaseUI.beginPhase();
		}
		else {
			myUI.selectedUnit = null;
			myUI.targetedUnit = null;
			myUI.repaint();
		}
		
		// Regardless, refresh the game
		myUI.game.endPhase();
	}
	
	public void updateTargetPanel() {
		if (targetStats == null) {
			targetStats = myUI.buildStatsPanel("Target", myUI.dim.width-200, myUI.dim.height-280);
		}
		if (myUI.targetedUnit != null) {
			((Label)targetStats.getComponent(1)).setText("Move Left: " + String.format("%.2f", myUI.targetedUnit.moveLeft));
			((Label)targetStats.getComponent(2)).setText("Strength: " + myUI.targetedUnit.getStrength());
			((Label)targetStats.getComponent(3)).setText("Defense: " + myUI.targetedUnit.getDefense());
			((Label)targetStats.getComponent(4)).setText("Attacks: " + myUI.targetedUnit.getAttacks());
			((Label)targetStats.getComponent(5)).setText("Wounds: " + myUI.targetedUnit.getWounds());
			((Label)targetStats.getComponent(6)).setText("Courage: " + myUI.targetedUnit.getCourage());
			((Label)targetStats.getComponent(7)).setText("FightValue: " + myUI.targetedUnit.getFightValue());
			((Label)targetStats.getComponent(8)).setText("ShootValue: " + myUI.targetedUnit.getShootValue());
			((Label)targetStats.getComponent(9)).setText("Mt/Wl/Ft: " + myUI.targetedUnit.getMight()+"/"+
					myUI.targetedUnit.getWill()+"/"+myUI.targetedUnit.getFate());
		}
		else {
			((Label)targetStats.getComponent(1)).setText("Move Left: -");
			((Label)targetStats.getComponent(2)).setText("Strength: -");
			((Label)targetStats.getComponent(3)).setText("Defense: -");
			((Label)targetStats.getComponent(4)).setText("Attacks: -");
			((Label)targetStats.getComponent(5)).setText("Wounds: -");
			((Label)targetStats.getComponent(6)).setText("Courage: -");
			((Label)targetStats.getComponent(7)).setText("FightValue: -");
			((Label)targetStats.getComponent(8)).setText("ShootValue: -");
			((Label)targetStats.getComponent(9)).setText("Mt/Wl/Ft: -/-/-");
		}
	}
	
	public void paint(Graphics bufferG) {
    	if (myUI.targetedUnit != null && myUI.targetedUnit.getWounds() > 0){
    		bufferG.setColor(Color.red);
    		bufferG.drawOval(myUI.targetedUnit.x-(myUI.targetedUnit.pixelRadius+1)-myUI.boardOffsetX, 
    						myUI.targetedUnit.y-(myUI.targetedUnit.pixelRadius+1)-myUI.boardOffsetY, 
    						2*(myUI.targetedUnit.pixelRadius+1), 2*(myUI.targetedUnit.pixelRadius+1));
    		bufferG.setColor(Color.black);
    	}
    	// Draw ovals for units able to shoot
    	((Graphics2D)bufferG).setStroke(new BasicStroke(2));
		bufferG.setColor(Color.ORANGE);
    	for (Unit u : myUI.game.armies[myUI.game.currArmy].units) {
    		if (u.shootRange > 0
    				&& u.moveLeft >= u.move * u.moveNeededToShoot
    				&& u.battle == null
    				&& !u.hasShot
    				&& !u.layingDown) {
    			bufferG.drawOval(u.x-(u.pixelRadius+2)-myUI.boardOffsetX, u.y-(u.pixelRadius+2)-myUI.boardOffsetY, 
    								2*(u.pixelRadius+2), 2*(u.pixelRadius+2));
    		}
    	}
    	bufferG.setColor(Color.BLACK);
		((Graphics2D)bufferG).setStroke(new BasicStroke());
		
		updateTargetPanel();
	}
}
